package com.meixiang.adapter.moments;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.moments.ReplyDetailList;
import com.meixiang.util.GlideHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/7 16:57
 */
public class ReplyDetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ReplyDetailList> mList=new ArrayList<>();

    private Context context;

    public ReplyDetailsAdapter(Context context) {
        this.context = context;
    }


    public void addAll(List<ReplyDetailList> mList){
        this.mList.addAll(mList);
        this.notifyDataSetChanged();
    }
    public void clear(){
        this.mList.clear();
        this.notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_reply, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView, pos);
                    // 点击item进入详情界面


                }
            });

        }
        if(mList!=null){
            ReplyDetailList mData = mList.get(position);

            if (mData.getAnswerName()==null){
                ((ViewHolder) holder).mTvOtherUserName.setVisibility(View.GONE);
                ((ViewHolder) holder).mTvUserReply.setVisibility(View.GONE);
            }else {

                ((ViewHolder) holder).mTvOtherUserName.setText(mData.getAnswerName());
            }

            GlideHelper.showCircleImage(context,mData.getMemberAvatar(),R.mipmap.default_head_portrait,((ViewHolder) holder).mIvUserHeadPortrait);
            ((ViewHolder) holder).mTvUserName.setText(mData.getMemberName());
            ((ViewHolder) holder).mTvTime.setText(mData.getCreateTime());
            ((ViewHolder) holder).mTvContent.setText(mData.getComment());
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_user_head_portrait)
        ImageView mIvUserHeadPortrait;
        @Bind(R.id.tv_user_name)
        TextView mTvUserName;
        @Bind(R.id.tv_user_reply)
        TextView mTvUserReply;
        @Bind(R.id.tv_other_user_name)
        TextView mTvOtherUserName;
        @Bind(R.id.tv_time)
        TextView mTvTime;
        @Bind(R.id.tv_date)
        TextView mTvDate;
        @Bind(R.id.tv_content)
        TextView mTvContent;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
